import Cookies from 'js-cookie'
import { getMenus } from '@/api/dashboard'

const app = {
  state: {
    sidebar: {
      opened: !+Cookies.get('sidebarStatus'),
      withoutAnimation: false
    },
    device: 'desktop',
    language: Cookies.get('language') || 'zh',
    menus: [],
  },
  mutations: {
    CLOSE_SIDEBAR: (state, withoutAnimation) => {
      Cookies.set('sidebarStatus', 1)
      state.sidebar.opened = false
      state.sidebar.withoutAnimation = withoutAnimation
    },
    TOGGLE_DEVICE: (state, device) => {
      state.device = device
    },
    SET_MENUS: (state, menus) => {
      state.menus = menus
    }
  },
  actions: {
    setLanguage({ commit }, language) {
      commit('SET_LANGUAGE', language)
    },
    // 获取左侧列表
    GET_MENUS({
      commit
    }) {
    return new Promise((resolve, reject) => {
      getMenus().then(resp => {
        if (resp.code === 200) {
          const data = resp.data;
          commit('SET_MENUS', data);
          resolve(resp);
        }
      }).catch(error => {
        reject(error)
      })
      })
    }
  }
}

export default app
